package com.order.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Order;
import com.common.entity.OrderShop;
import com.common.entity.Shop;
import com.common.untils.CommonResult;
import com.order.mapper.OrderShopMapper;
import com.order.service.IOrderShopService;
import com.order.vo.OrderShopVo;
import com.order.vo.UserOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xdl
 * @since 2022-09-03
 */
@Service
public class OrderShopServiceImpl extends ServiceImpl<OrderShopMapper, OrderShop> implements IOrderShopService {
    @Resource
    private OrderShopMapper orderShopMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult findAllDetail(String id) {
        QueryWrapper<OrderShop> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("oid",id);
        List<OrderShop> orderShops = baseMapper.selectList(objectQueryWrapper);
        return new CommonResult(2000,"查询成功",orderShops);
    }

    @Override
    public CommonResult orderShop(OrderShopVo orderShopVo,String homeToken) {
        Page<Order> page = new Page(orderShopVo.getCurrentPage(),orderShopVo.getPageSize());
        QueryWrapper<Order> queryWrapper = new QueryWrapper();
        if(StringUtils.hasText(orderShopVo.getGoodsname())){
            queryWrapper.like("goodsname",orderShopVo.getGoodsname());
        }if(StringUtils.hasText(orderShopVo.getBeginTime())){
            queryWrapper.ge("order_date",orderShopVo.getBeginTime());
        }if(StringUtils.hasText(orderShopVo.getEndTime())){
            queryWrapper.le("order_date",orderShopVo.getEndTime());
        }
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String bid = client.getString("bid");
        Shop shop = orderShopMapper.findShopId(bid);
        Integer sid = shop.getId();
        System.out.println("++++++++++++++++++"+sid);
        queryWrapper.eq("t.shop_id",sid);
        IPage<Map> iPage= orderShopMapper.orderShop(page,queryWrapper);
        return new CommonResult(2000,"查询成功",iPage);
    }

    //用户订单查询
    @Override
    public CommonResult userOrder02(UserOrderVo userOrderVo, String homeToken) {
        //token获取当前登录着id
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String bid = client.getString("id");

        Page<Map> page = new Page<>(userOrderVo.getPageCurrent(),userOrderVo.getPageSize());
        QueryWrapper<Map> objectQueryWrapper = new QueryWrapper<>();
        if (userOrderVo.getState()!=null){
            objectQueryWrapper.eq("o.state",userOrderVo.getState());
        }
        objectQueryWrapper.eq("o.uid",bid);
        objectQueryWrapper.eq("o.is_deleted",0);
        IPage<Map> iPage=orderShopMapper.userOrder(page,objectQueryWrapper);
        return new CommonResult(2000,"查询成功",iPage);
    }

}
